var ns, app, dialog, toast;
app = new Vue({
    el: '#app',
    data: {
        // IsAccept:同意,IsReject:拒绝,IsRevoke:撤销,
        list: [],
        form: {
            ID:0,
            pageindex: 0,
            pagesize: 10,
            keywords:'',
            status: 1, //0-全部 1-进行中 2-我发起的 3-我参与的 4-已完成
            selectType:[],
        },
        menuType:[],//审批类型
        current_item_length:0,
        total:0,
        can_scroll: false,
        is_searching: false,
        showsearch:false,
        showTop:false,
        showsearchresult:false,
        searchresult:'',
        selectlist:[],

        oldScrolltop:0,
        scrollFlag:true,
        Interval:null,
        firstID:0,
        chooseexaminetype:{},
        choosetaskmodel:{},
        optionName:'',
        choosesortmodel:{},
        option2: [
            { count:20,text: '全部任务', value: 0 },
            { count:5,text: '进行中', value: 1 },
            { count:6,text: '我发起的', value: 2 },
            { count:7,text: '我参与的', value: 3 },
            { count:3,text: '已完成', value: 4 },
        ],
        option3: [
            // { text: '排序', value: 0 },
            { text: '时间近到远', value: 1 },
            { text: '时间远到近', value: 2 },
            
        ],
        Type:0,
        OrderByType:0,
        
    },

    methods: {
        get_data: function () {
            var that = this;
            that.is_searching = true;
            that.can_scroll = false;
            var options = {};
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.Type = that.Type;
            options.Status = that.form.status;
            options.OrderByType = that.OrderByType;
            options.action = "getapprovaltasklist";
            console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                console.log(JSON.stringify(data));
                that.is_searching = false;
                if (succeed) {
                    // that.selectlist=data.list;
                    if (data.list.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.selectlist = data.list;
                    } else {
                        that.selectlist = that.selectlist.concat(data.list);
                    }
                    app.current_item_length = that.selectlist.length;
                    for(var i=0;i<data.list.length;i++){
                        data.list[i].AddTime=ns.SetTime_style(data.list[i].AddTime);
                    }
                } else if (err) {
                    that.list = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        //获取筛选条件
        get_menuType:function(){
            var that=this;
            var options = {};
            options.action = 'getapprovalpendingtasklist';
            ns.post(options, function (succeed, data, err) {
                console.log(JSON.stringify(data));
                if(succeed){
                    if(data.status){
                        that.menuType=data.TypeList;
                        that.option2=data.list;
                        that.chooseexaminetype=data.TypeList[0];
                        that.choosetaskmodel=data.list[1];
                        that.optionName=that.choosetaskmodel.Name+'('+that.choosetaskmodel.Number+')';
                        that.choosesortmodel=that.option3[0];

                    }
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        onChooseType:function(item){
            var that = this;
            that.chooseexaminetype=item;
            this.$refs.choosetype.toggle();
            that.Type=item.Type;
            that.get_data();
        },
        onChooseTask:function(item){
            var that = this;
            that.choosetaskmodel=item;
            that.optionName=that.choosetaskmodel.Name+'('+that.choosetaskmodel.Number+')';
            this.$refs.choosetask.toggle();
            that.form.status=item.Type;
            that.get_data();
        },
        onChooseSort:function(item){
            var that = this;
            that.choosesortmodel=item;
            this.$refs.choosesort.toggle();
            that.OrderByType=item.value;
            that.get_data();
        },




        //翻页第一条数据的位置
        do_scrollbtn:function(id){
            　　　　 var sollid = document.getElementById('scroll'+id);
            　　　　 window.scrollTo(0,sollid.offsetTop); 
            　　},

        select_status: function (status) {
            var that = this;
            that.showTop=false;
            that.showsearchresult=true;
            if(status==1){
                that.searchresult='采购申请';
                that.form.selectType=[1];
            }
            if(status==2){
                that.searchresult="优惠申请";
                that.form.selectType=[2];
            }
            if(status==9){
                that.searchresult="通知公告";
                that.form.selectType=[9];
            }
            if(status==10){
                that.searchresult="房屋租售";
                that.form.selectType=[10];
            }
            if(status==11){
                that.searchresult="社区活动";
                that.form.selectType=[11];
            }
            if(status==12){
                that.searchresult="社区新闻";
                that.form.selectType=[12];
            }
            if(status==13){
                that.searchresult="问卷调查";
                that.form.selectType=[13];
            }
            if(status==14){
                that.searchresult="社区投票";
                that.form.selectType=[14];
            }
            if(status==15){
                that.searchresult="生活服务";
                that.form.selectType=[15];
            }
            if(status==16){
                that.searchresult="放行条";
                that.form.selectType=[16];
            }
            if(status==17){
                that.searchresult="装修登记";
                that.form.selectType=[17];
            }
            if(status==18){
                that.searchresult="外包合同";
                that.form.selectType=[18];
            }
            if(status==19){
                that.searchresult="外包合同付款";
                that.form.selectType=[19];
            }
            if(status==20){
                that.searchresult="外包合同罚款";
                that.form.selectType=[20];
            }
            if(status==21){
                that.searchresult="员工入职";
                that.form.selectType=[21];
            }
            if(status==22){
                that.searchresult="员工转正";
                that.form.selectType=[22];
            }
            if(status==23){
                that.searchresult="员工离职";
                that.form.selectType=[23];
            }
            if(status==24){
                that.searchresult="员工调动";
                that.form.selectType=[24];
            }
            if(status==25){
                that.searchresult="员工奖励";
                that.form.selectType=[25];
            }
            if(status==26){
                that.searchresult="员工处罚";
                that.form.selectType=[26];
            }
            if(status==27){
                that.searchresult="员工劳动合同";
                that.form.selectType=[27];
            }
            if(status==28){
                that.searchresult="员工休假";
                that.form.selectType=[28];
            }
            that.form.pageindex=0;
            
            that.list = [];
            that.get_data();
        },
        do_closearch:function(){
            var that=this;
            that.showsearchresult=false;
            that.form.selectType=[];
            that.list = [];
            that.get_data();

        },
        do_search: function() {
            this.get_data();
        },

        openGongdan: function (ApprovalID,ItemType) {
            var that = this;
            if(ItemType==18||ItemType==19||ItemType==20){
                ns.openWin('examine_detail_hetong', '合同详情', {
                    ApprovalID: ApprovalID,
                    Type:ItemType
                });

            }else if(ItemType==21||ItemType==22||ItemType==23||ItemType==24||ItemType==25||ItemType==26||ItemType==27||ItemType==28||ItemType==29||ItemType==30){
                ns.openWin('examine_detail_yg_bangong', '审批详情', {
                    ApprovalID: ApprovalID,
                    Type:ItemType
                });
            }
            else{
                ns.openWin('examine_detail', '审批详情', {
                    ApprovalID: ApprovalID,
                    Type:ItemType
                });

            }
        },
        //同意
        do_open_sure:function(ApprovalID){
            var that = this;
            var options = {};
            options.ApprovalID = ApprovalID;
            options.Type=1;
            options.action = "getisoperate";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status == true) {
                        ns.openWin('examine_yes', '审批建议', {
                            IsReject: 0,
                            ApprovalID: ApprovalID,
                        });
                    } else {
                        api.toast({
                            msg: '状态已改变，请重新选择',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        select_allstatus:function(){
            var that=this;
            that.showsearch=!that.showsearch;
            that.showTop=false;
            var options={};
            options.typeList = JSON.stringify(that.form.selectType);
            options.action = "getapprovalpendinglist";
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    that.selectlist=data.list;
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }

            });
            

        },
        
        open_select:function(){
            var that=this;
            that.showTop=!that.showTop;
            that.showsearch=false;

        },
        doHideSearch:function(){
            var that=this;
            that.showTop=false;
            that.showsearch=false;
        },
        //拒绝
        do_open_jujue:function(ApprovalID){
            var that = this;
            var options = {};
            options.ApprovalID = ApprovalID;
            options.Type=2;
            options.action = "getisoperate";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status == true) {
                        ns.openWin('examine_yes', '审批建议', {
                            IsReject: 1,
                            ApprovalID: ApprovalID,
                        });
                    } else {
                        api.toast({
                            msg: '状态已改变，请重新选择',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });

        },
        //撤销
        doRevoke:function(ApprovalID){
            var that=this;
            var options = {};
            options.ApprovalID = ApprovalID;  
            options.action = "approvalrevoke";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status == true) {
                        api.toast({
                            msg: '撤销成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();
                    } else if (err) {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        //搜索结果返回位置
        do_scrollTo:function(){
            var that = this;
    　　　　 var sollid = document.getElementById('scroll'+that.form.ID);
    　　　　 window.scrollTo(0,sollid.offsetTop); 
            var classlist=document.getElementById('scroll'+that.form.ID).getElementsByClassName('boxbg')[0];
            classlist.style.backgroundColor = "#eaf1ff";
            setTimeout(function() {
                classlist.style.backgroundColor = "#f7fbff";
            }, 800)
    　　},
    }
});

apiready = function () {

    api.parseTapmode();

    ns = window.Foresight.Util;

    dialog = new auiDialog();

    toast = new auiToast();
    app.get_menuType();
    app.form.servicetype = api.pageParam.servicetype || 1;
    app.form.ID = api.pageParam.ID || 0;
 
    app.form.RoomID = api.pageParam.id || 0;

    app.form.isviewdetail = api.pageParam.isviewdetail || false;

    if (app.form.isviewdetail) {
        app.form.status = 4;
    }

    app.form.onlybaoshi = api.pageParam.onlybaoshi || 0;


    ns.init_pull();

    // ns.init_search();

    app.get_data();

    api.addEventListener({
        name: 'reloadexaminelist'
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });
    api.addEventListener({
        name: 'customerservicelist_frm'
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });
    api.addEventListener({
        name: 'is_hidden_right',
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });
    api.addEventListener({
        name: 'do_open_select'
    }, function(ret, err) {
        app.open_select();
    });

    // api.addEventListener({
    //     name: 'do_open_service_jindu'
    // }, function(ret, err) {
    //     app.doOpenServiceProcess();
    // });
};
